package com.sinodata.bsm.common.vo;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * RealTimeViewConf entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "REAL_TIME_VIEW_CONF")
@NamedQueries({ @NamedQuery(name = "RealTimeViewConf.findAll", query = "SELECT r FROM RealTimeViewConf r"), @NamedQuery(name = "RealTimeViewConf.findById", query = "SELECT r FROM RealTimeViewConf r WHERE r.id = :id"), @NamedQuery(name = "RealTimeViewConf.findByResTypeId", query = "SELECT r FROM RealTimeViewConf r WHERE r.resTypeId = :resTypeId"), @NamedQuery(name = "RealTimeViewConf.findByResId", query = "SELECT r FROM RealTimeViewConf r WHERE r.resId = :resId") })
public class RealTimeViewConf implements java.io.Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 5525489381297250876L;

    // Fields    
    private Long id;
    private Long resTypeId;
    private Long resId;
    private String xml;
    private String page;
    private Date modifyTime;

    // Constructors

    /** default constructor */
    public RealTimeViewConf() {
    }

    /** minimal constructor */
    public RealTimeViewConf(Long id) {
        this.id = id;
    }

    /** full constructor */
    public RealTimeViewConf(Long id, Long resTypeId, Long resId, String xml, Date modifyTime) {
        this.id = id;
        this.resTypeId = resTypeId;
        this.resId = resId;
        this.xml = xml;
        this.modifyTime = modifyTime;
    }

    // Property accessors
    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 18, scale = 0)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence")
    @SequenceGenerator(name = "sequence", sequenceName = "TOPU_CONF_SEQ")
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "RES_TYPE_ID", precision = 18, scale = 0)
    public Long getResTypeId() {
        return this.resTypeId;
    }

    public void setResTypeId(Long resTypeId) {
        this.resTypeId = resTypeId;
    }

    @Column(name = "RES_ID", precision = 18, scale = 0)
    public Long getResId() {
        return this.resId;
    }

    public void setResId(Long resId) {
        this.resId = resId;
    }

    @Column(name = "XML")
    public String getXml() {
        return this.xml;
    }

    public void setXml(String xml) {
        this.xml = xml;
    }

    @Column(name = "MODIFY_TIME", length = 7)
    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    @Column(name = "PAGE")
    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    @Override
    public String toString() {
        return "RealTimeViewConf [id=" + id + ", resTypeId=" + resTypeId + ", resId=" + resId + ", xml=" + xml + ", page=" + page + ", modifyTime=" + modifyTime + "]";
    }

    public String customName() {
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(new ByteArrayInputStream(xml.getBytes("utf-8")));
            Element root = doc.getRootElement();
            return root.attributeValue("name");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}